import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/element/LoginView.vue'
import IndexView from '../views/element/IndexView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'LoginView',
    component: LoginView
  },
  {
    path: '/index',
    name: 'IndexView',
    component: IndexView,
    
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,_from,next)=>{
  let refreshToken = localStorage.getItem("refreshToken");
  console.log(refreshToken);
  if(to.path==='/'&&refreshToken!=null){
    next('/index');
    return;
  }
  if(!(to.path==='/')&&refreshToken===null){
    Vue.prototype.$message.warning('请先登录!');
    next('/');
    return;
  }
  next(true)
})

export default router
